//
//  SXPOriginal.swift
//  weibo
//
//  Created by shixinPeng on 16/3/1.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/*
原创微博 --
头像
认证图片
微博名字
会员等级
发表时间
发表来源
发表内容
*/
/// 原始微博间隔
let cellMargin = 10


class SXPOriginal: UIView {

    //定义一个约束属性
    var bottomConstraint:Constraint?
    
    //定义一个属性,利用set方法赋值
    var statusesViewModel: SXPstatusesViewModel? {
        didSet{
            //赋值
            nameLabel.text = statusesViewModel?.statusesModel?.user?.screen_name ?? ""
            
            //原创微博内容赋值
            
            if let text = statusesViewModel?.statusesModel?.text{
                
                contentLabel.attributedText  = SXPstatusesViewModel().getAttributdString(text)
            }
            
            
            veriftyImageView.image = statusesViewModel?.verifty_Image
            
            levelImageView.image = statusesViewModel?.level_Image
            
            let url = NSURL(string: statusesViewModel?.statusesModel?.user?.profile_image_url ?? "")
            
            if let Ourl = url {
                
                PhotoImageView.sd_setImageWithURL(Ourl)
            }
            
            
            sourceLabel.text  = statusesViewModel?.dealSouce(statusesViewModel?.statusesModel?.source)
            
            timeLabel.text = statusesViewModel?.dealCreatedDate(statusesViewModel?.statusesModel?.created_at)
            
            //移除多余约束
            bottomConstraint?.uninstall()
            if let pic_urls = statusesViewModel?.statusesModel?.pic_urls where pic_urls.count > 0{
               
                //有图片
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    
                })
              //进行传值
                //创建一个临时数组进行拼接
                var result = [SXPpictureViewModel]()
                
                for url in pic_urls {
                    //实例化一个模型
                    let viewModel = SXPpictureViewModel()
                    
                    viewModel.pictureModel = url
                    
                    //添加到临时数组中
                    result.append(viewModel)
                }
                //进行传值
                pictureView.pictureList = result
   
            }else{
                //没有图片
                pictureView.hidden = true
                //修改约束
              
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint

                })
            }
            
        }
    }
    ///MARK:自定义控件第一步:重写初始化方法

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    ///MARK: 自定义控件第二步:创建私有方法,添加控件和设置约束
    func setUpUI() {
        //添加控件
        addSubview(PhotoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加配图控件
        addSubview(pictureView)
        
        //设置约束
        //设置头像的约束:左 5 上 5 宽/高 40
        PhotoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(cellMargin)
            make.top.equalTo(self.snp_top).offset(cellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
        }
        //设置等级图片的约束 中心点
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(PhotoImageView.snp_right)
            make.centerY.equalTo(PhotoImageView.snp_bottom)
            //make.size.equalTo(CGSizeMake(10, 10))
        }
        //设置微博名称的图片约束 左边 上边
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(PhotoImageView.snp_right).offset(cellMargin)
            make.top.equalTo(PhotoImageView.snp_top)
            
        }
        //设置会员等级的图片约束 左边  中心线
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(cellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        //设置时间标签约束 左边 上边
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(PhotoImageView.snp_right).offset(cellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(cellMargin/2)
        }
        //设置来源标签的约束 左边 中心线
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(cellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        //设置微博内容的约束 左边 上边
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(cellMargin)
            make.top.equalTo(PhotoImageView.snp_bottom).offset(cellMargin)
            make.width.equalTo(UIScreen.mainScreen().bounds.size.width-2*CGFloat(cellMargin))
            //make.bottom.equalTo(pictureView.snp_top)
            
        }
        //设置配图的约束 上 和左
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(self.snp_left).offset(cellMargin)
            
        }
        //需要告诉视图底部在哪里
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    ///MARK: 自定义控件第三步:懒加载
    ///头像
    private lazy var PhotoImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    }()
    ///认证图片
    private lazy var veriftyImageView: UIImageView = {
        let img = UIImageView()
        
        //img.image = UIImage(named: "tabbar_profile")
        
        return img
    }()
    ///微博名字
    private lazy var nameLabel:UILabel = {
       let lab = UILabel()
        
        lab.text = "微博名字"
        
        lab.font = UIFont.systemFontOfSize(15)
        
        return lab
    }()
    ///会员等级
    private lazy var levelImageView: UIImageView = {
       let img = UIImageView()
        
        img.image = UIImage(named: "error@3x")
        
        return img
    }()
    ///发表时间
    private lazy var timeLabel:UILabel = {
       let lab = UILabel()
        
        lab.text = "发表时间"
        
        lab.font = UIFont.systemFontOfSize(13)
        
        lab.textColor = UIColor.orangeColor()
        
        return lab
    }()
    ///发表来源
    private lazy var sourceLabel: UILabel = {
      let lab = UILabel()
        
        lab.text = "发表来源"
        
        lab.font = UIFont.systemFontOfSize(13)
        
        return lab
        
    }()
    ///发表内容
    private lazy var contentLabel: UILabel = {
        let lab = UILabel()
        
        //lab.text = "微博内容"
        
        lab.font = UIFont.systemFontOfSize(16)
        
        lab.numberOfLines = 0
        
        return lab
    }()

    ///配图
    private lazy var pictureView:SXPpictureView = SXPpictureView()
}
